package com.xinyue.ext.sql;

import java.io.PrintWriter;
import java.util.List;

import com.xinyue.ext.EnumData;
import com.xinyue.ext.data.EntityData;
import com.xinyue.ext.data.FieldData;
import com.xinyue.ext.util.StringUtil;

public class MySqlDB extends SqlContext implements EnumData{
	//私有构造器
	private MySqlDB() {
	}
	//内部类
	private static class SingletonHolder{
        private static MySqlDB instance = new MySqlDB();  
    }
	/**
	 * 获得对象实例
	 */
	public static MySqlDB getInstance(EntityData entity) {
		SqlContext.entity = entity;
		return SingletonHolder.instance;
	}


	/**
	 * 创建符合MySQL规范的sql语句
	 */
	@Override
	public void createSql(PrintWriter pw) {

		//建表语句
		pw.println("drop table if exists " + entity.getTable() + ";");
		pw.println("create table " + entity.getTable() + "(");
		//id
		FieldData id = entity.getId();
		
		if(id.isAuto_key()) {
			pw.println(buildField(id) + " auto_increment primary key,");
		}else {
			pw.println(buildField(id) + ",");
		}
		//字段
		List<FieldData> fd = entity.getFields();
		for (int i = 0; i < fd.size() - 1; i++) {
			pw.println(buildField(fd.get(i)) + ",");
		}
		pw.println(buildField(fd.get(fd.size() - 1)) + ",");
		
		pw.println("};");
		pw.println();
	}
	
	/**
	 * 构建符合mySql规范的sql语句
	 * @return
	 */
	private String buildField(FieldData fd) {
		String def = fd.getDefinition();
		//自定义类型则直接返回
		if(!StringUtil.isNull(def)) {
			return def;
		}
		
		String type = fd.getType();
		String len = fd.getLength();
		boolean notNull = fd.isNullable();
		boolean unique = fd.isUnique();
		
		if(javaType[0].equals(type)) {//Data
			if(notNull) {
				type = mySQLType[0];
			}else {
				type = mySQLType[0] + " not null";
			}
		}else if(javaType[1].equals(type)) {//BigDecimal
			type = mySQLType[1] + "(19,2)";
		}else if(javaType[2].equals(type)) {//Blob
			type = mySQLType[2];
		}else if(javaType[3].equals(type)) {//String
			if(StringUtil.isNull(len)) {
				type = mySQLType[3] + "(255)";
			}else {
				type = mySQLType[3] + "(" + len + ")";
			}
		}else if(javaType[4].equals(type)) {//boolean
			type = mySQLType[4] + " not null";
		}else if(javaType[5].equals(type)) {//int
			if(StringUtil.isNull(len)) {
				type = mySQLType[5] + " not null";
			}else {
				type = mySQLType[5] + "(" + len + ")" + " not null";
			}
		}else if(javaType[6].equals(type)) {//double
			type = mySQLType[6] + " not null";
		}else if(javaType[7].equals(type)) {//long
			if(StringUtil.isNull(len)) {
				type = mySQLType[7] + " not null";
			}else {
				type = mySQLType[7] + "(" + len + ")" + " not null";
			}
		}else if(javaType[8].equals(type)) {//float
			type = mySQLType[8] + " not null";
		}else if(javaType[9].equals(type)) {//byte
			type = mySQLType[9] + " not null";
		}else if(javaType[10].equals(type)) {//short
			type = mySQLType[10] + " not null";
		}else if(javaType[11].equals(type)) {//char
			type = mySQLType[11] + "(1) not null";
		}
		
		if(unique) {
			return fd.getName() + " " + type + " unique";
		}
		return fd.getName() + " " + type;
	}
}
